<?php namespace Common\Model;
use Hdphp\Model\Model;
/**
* 用户模型
*/
class Homeuser extends Model
{
	protected $table="homeuser";
	protected $validate=array(
		 array('username','required','用户名不能为空',3,3),
		 array('name','required','昵称不能为空',1,1),
		 array('password','required','密码不能为空',3,3),
		 array('email','email','邮箱格式不正确',1,1)
		);
	//验证登陆
	public function vLogin($username,$password)
	{
		if(!$this->create()) return false;
		//判断用户名是否存在
		$data=$this->where("username='{$username}'")->find();
		if (!$data) {
			//错误信息
			$this->error="用户名{$username}不存在";
			return false;
		}
		$password=$this->encrypt($username,$password);
		if ($password!=$data['password']) {
			$this->error="密码错误";
			return false;
		}
		//存session
		$_SESSION['hname']=$data['name'];
		$_SESSION['id']=$data['id'];
		return true;
	}
	//注册新用户
	public function store($username,$password,$password1)
	{
		if(!$this->create()) return false;
		//判断用户名是否存在
		$data=$this->where("username='{$username}'")->find();
		if ($data) {
			$this->error="用户名{$username}已存在";
			return false;
		}
		if($password!=$password1){
			$this->error="两次密码不一样";
			return false;
		}
		$password=$this->encrypt($username,$password);
		$this->add(array(
			'name'=>$_POST['name'],
			'email'=>$_POST['email'],
			'username'=>$username,
			'password'=>$password
			));
		return true;
	}
	//加密
	public function encrypt($username,$password){
		return md5(md5($username) . md5($password) . 'houdunwang');
	}
}

 ?>